package com.scau.controller;

import com.google.common.base.Preconditions;
import com.scau.common.annotations.CurrentUser;
import com.scau.common.message.ResponseMessage;
import com.scau.common.util.MessageUtil;
import com.scau.entity.Contract;
import com.scau.entity.Counselor;
import com.scau.service.CounselorService;
import com.scau.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 顾问
 */
@RestController
@RequestMapping("counselors")
public class CounselorController {

    @Autowired
    private CounselorService counselorService;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResponseMessage<String> login(@RequestBody Counselor counselor, HttpServletRequest request) throws Exception {

        String address = SessionUtil.getIpAddr(request);
        Preconditions.checkNotNull(counselor.getPhone(), "账号不能为空");
        Preconditions.checkNotNull(counselor.getPassword(), "密码不能为空");

        counselorService.login(address, counselor);

        return MessageUtil.getSuccessMessage();

    }

    /**
     * 添加
     *
     * @param counselor
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseMessage<Counselor> create(@RequestBody Counselor counselor) {

        Preconditions.checkNotNull(counselor.getPhone(), "phone can not be null");
        Preconditions.checkNotNull(counselor.getPassword(), "password can not be null");

        counselorService.add(counselor);

        return MessageUtil.getSuccessMessage(counselor);
    }

    /**
     * 按Id查询
     *
     * @param counselorId
     * @return
     */
    @RequestMapping(value = "/{counselorId}", method = RequestMethod.GET)
    public ResponseMessage<Counselor> get(@PathVariable Long counselorId) {

        Counselor counselor = counselorService.findOne(counselorId);

        return MessageUtil.getSuccessMessage(counselor);
    }

    /**
     * 按条件查询
     *
     * @param counselor
     * @param pageable
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseMessage<Page<Counselor>> list(Counselor counselor, Pageable pageable) {

        Page<Counselor> counselors = counselorService.findAll(counselor, pageable);

        return MessageUtil.getSuccessMessage(counselors);
    }

    @RequestMapping(value = "/{counselorId}", method = RequestMethod.PUT)
    public ResponseMessage<Counselor> update(@PathVariable Long counselorId, @RequestBody Counselor counselor) {

        counselor.setId(counselorId);

        Counselor newCounselor = counselorService.update(counselor);

        return MessageUtil.getSuccessMessage(newCounselor);
    }

    @RequestMapping(value = "/{counselorId}", method = RequestMethod.DELETE)
    public ResponseMessage<String> delete(@PathVariable Long counselorId) {
        counselorService.delete(counselorId);

        return MessageUtil.getSuccessMessage("删除成功");
    }

    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public ResponseMessage<Counselor> detail(@CurrentUser(type = CurrentUser.Type.COUNSELOR) Counselor counselor) {
        return MessageUtil.getSuccessMessage(counselor);
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ResponseMessage<Counselor> edit(@CurrentUser(type = CurrentUser.Type.COUNSELOR) Counselor counselor,
                                           Counselor newCounselor) {

        newCounselor.setId(counselor.getId());
        newCounselor = counselorService.update(newCounselor);
        return MessageUtil.getSuccessMessage(newCounselor);
    }
}
